//
//  UpgradeModule.m
//  AksFireApp
//
//  Created by mac on 2022/5/5.
//

#import "UpgradeModule.h"
#import <UIKit/UIKit.h>
#import "UpgradeView.h"
#import "LEEAlert.h"
@interface UpgradeModule ()



@end



@implementation UpgradeModule

- (dispatch_queue_t)methodQueue
{
    return dispatch_get_main_queue();
}




RCT_EXPORT_MODULE();


RCT_EXPORT_METHOD(getAppVersion:(RCTPromiseResolveBlock)resolve
                          reject:(RCTPromiseRejectBlock)reject){
      NSDictionary *infoDict = [[NSBundle mainBundle] infoDictionary];
      NSString *currentVersion = [infoDict objectForKey:@"CFBundleShortVersionString"];
      resolve(currentVersion);
}



RCT_EXPORT_METHOD(getOSVersion:(RCTPromiseResolveBlock)resolve
                          reject:(RCTPromiseRejectBlock)reject){
      UIDevice *device = [UIDevice currentDevice];
      NSString *systemVersion = [NSString stringWithFormat:@"%@ %@",device.systemName,device.systemVersion];
      resolve(systemVersion);
}




RCT_EXPORT_METHOD(upgrade:(NSDictionary *)info
                  resolve:(RCTPromiseResolveBlock)resolve
                          reject:(RCTPromiseRejectBlock)reject){//checkCode, desc

  NSString * version = info[@"version"];
  NSString * title = info[@"title"];
  NSString * downloadUrl = info[@"downloadUrl"];
  NSString * note = info[@"note"];
  BOOL force = info[@"force"];

  NSString *currentVersion = [self getLocalVersion];
  NSComparisonResult result =[self buildVersion:currentVersion compareWithAppstoreVersion:version];
  if( result== NSOrderedAscending){
    dispatch_async(dispatch_get_main_queue(), ^{
        CGFloat SCREEN_WIDTH =   [UIScreen mainScreen].bounds.size.width;
        UpgradeView *view =  [[UpgradeView alloc] initWithTitle:title note:note force:force];
        view.closeBlock = ^{
            [LEEAlert closeWithCompletionBlock:nil];
        };
        view.settingBlock = ^{
          [[UIApplication sharedApplication] openURL:[NSURL URLWithString: [downloadUrl stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]] options:@{} completionHandler:nil];
        };

        [LEEAlert alert].config
        .LeeMaxWidth(SCREEN_WIDTH-40)
        .LeeCustomView(view)
        .LeeHeaderInsets(UIEdgeInsetsMake(0, 0, 0, 0))
        .LeeItemInsets(UIEdgeInsetsMake(0, 0, 0, 0))
        #ifdef __IPHONE_13_0
        .LeeUserInterfaceStyle(UIUserInterfaceStyleLight)
        #endif
        .LeeShow();
      
    });
  }
  else if(result == NSOrderedDescending){
        reject(@"error_app_upgrade",@"商城版本低于本地版本",NULL);
  }else if(result == NSOrderedSame){
        reject(@"error_app_upgrade",@"已是最新版本",NULL);
  }else{
        resolve(@"发现新版本");
  }

 
}

- (NSString*) getLocalVersion {
  return [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
}

/**
 版本比较
 */
-(NSComparisonResult)buildVersion:(NSString*)buildVersion compareWithAppstoreVersion:(NSString*)appStoreVersion{
  NSArray* buildVersionNums = [buildVersion componentsSeparatedByString:@"."];
  NSArray* appStoreVersionNums = [appStoreVersion componentsSeparatedByString:@"."];
  for (NSInteger i=0; i<buildVersionNums.count; i++) {
    NSInteger buildNum = [buildVersionNums[i] integerValue];
    if(i<appStoreVersionNums.count){
      NSInteger appStoreNum = [appStoreVersionNums[i] integerValue];
      if(buildNum < appStoreNum){
        return  NSOrderedAscending;
      }else if(buildNum>appStoreNum){
        return  NSOrderedDescending;
      }
    }
  }
  
  if(buildVersionNums.count > appStoreVersionNums.count){
    return NSOrderedDescending;
  }else if(buildVersionNums.count < appStoreVersionNums.count){
    return  NSOrderedAscending;
  }
  return NSOrderedSame;
}


+ (BOOL)requiresMainQueueSetup {
  return YES;
}
@end
